package com.h3bpm.web.utils;

import com.aspose.cells.License;
import com.aspose.cells.Workbook;
import com.aspose.words.*;
import org.omg.CORBA.Environment;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

public class Office2Pdf {


    public static  final int word = 1;
    public static  final int excel = 2;

    /**
     *   word 和 excel 转pdf
     * @param bytes
     * @param fileType
     * @return
     * @throws Exception
     */
    public static byte[] toPdf(byte [] bytes,int fileType) throws Exception {
        byte [] byteArr = null;
        if (bytes == null ) {
            return null;
        }



        if (fileType==word  ) {
            byteArr =  word2Pdf(bytes);
        } else if (fileType ==excel) {
            byteArr = excel2Pdf(bytes);
        }

        return byteArr;
    }


    /**
     *  word to pdf
     * @param bytes
     * @return
     * @throws Exception
     */
    private static byte[] word2Pdf (byte[] bytes) throws Exception {

        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);

        Document doc = new Document(bis);
        // 接受doc最终版
        doc.acceptAllRevisions();

        ByteArrayOutputStream os = new ByteArrayOutputStream();

        doc.save(os, SaveFormat.PDF);

        byte[] byteArray =  os.toByteArray();
        ioClose(bis);
        ioClose(os);



        return byteArray;

    }




    /**
     *  excel to pdf
     * @param  bytes
     * @return
     * @throws Exception
     */
    public static byte[] excel2Pdf (byte[] bytes) throws Exception {

        License license = new License();
        license.setLicense("/lib/license.xml");

        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        Workbook wb = new Workbook(bis);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        wb.save(bos, com.aspose.cells.SaveFormat.PDF);

        byte[] byteArray =  bos.toByteArray();

        ioClose(bis);

        ioClose(bos);

        return byteArray;

    }





    private static void ioClose(Object io){

        if (io == null) {
            return;
        }
        try {
            if (io instanceof InputStream) {
                ((InputStream) io).close();
            } else if (io instanceof OutputStream) {
                ((OutputStream) io).close();
            }else {
                System.out.println("io is not io");
            }

        } catch (Exception e) {
            System.out.println("io close fail");
        }


    }


    // 锚点变量替换,doc可完全处理，docx会发生段落格式更改
    public static byte[] wordReplace(byte[] bytes,String fileName, Map<String,Object> param) throws Exception {
        int saveFormat = 0;
        fileName = fileName.toLowerCase();
        if (fileName.endsWith(".doc")){
            saveFormat = SaveFormat.DOC;
        }else if(fileName.endsWith(".docx")){
            saveFormat = SaveFormat.DOCX;
        }else{
            return bytes;
        }

        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        Document doc = new Document(bis);//加载模板
        Range range = doc.getRange();


        String str = "${%s}";
        for (Map.Entry<String,Object> entry:param.entrySet()) {
            range.replace(String.format(str,  entry.getKey()), entry.getValue().toString(),false,false);
        }

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        doc.save(bos,SaveOptions.createSaveOptions(saveFormat));

        byte[] resultByte = bos.toByteArray();
        bis.close();
        bos.close();

        return resultByte;
    }



}
